package com.jrx.anytxn.param.controller.product;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.product.PrAutoPaymentTableRep;
import com.jrx.anytxn.param.dto.product.PrAutoPaymentTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IPrAutoPaymentTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 批扣配置表
 *
 * @author lbz
 * @date 2019/12/5
 */
@Api(tags = "批扣配置参数API接口")
@RestController
@RequestMapping(value = "/param/prAutoPayment")
public class PrAutoPaymentController {
    private static final Logger logger = LoggerFactory.getLogger(PrAutoPaymentController.class);

    @Autowired
    private InfoService infoService;
    @Autowired
    private IPrAutoPaymentTableService prAutoPaymentTableService;

    @ApiOperation(value = "根据租户ID和产品ID查询信息", notes = "根据租户ID和产品ID查询信息")
    @GetMapping(value = "/{tenantId}/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "id", value = "渠道产品批扣配置表主键", required = true)
    })
    public TxnRespResult<PrAutoPaymentTableRes> getByTenIdAndProductId(@NotNull @PathVariable String tenantId, @PathVariable String id) {
        TxnRespResult<PrAutoPaymentTableRes> result = new TxnRespResult<>();
        PrAutoPaymentTableRes res;
        try {
            res = prAutoPaymentTableService.findByTenIdAndProductId(String.valueOf(infoService.getUser().getProjectId()), id);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrAutoPaymentTableRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PrAutoPaymentTableRes>> result = new TxnRespResult<>();
        TxnPage<PrAutoPaymentTableRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = prAutoPaymentTableService.findAllPageByTenantId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增批扣配置", notes = "新增批扣配置API")
    @PostMapping("/")
    public TxnRespResult<PrAutoPaymentTableRes> createPrAutoPaymentTable(@RequestBody PrAutoPaymentTableRep prAutoPaymentTableRep) {
        TxnRespResult<PrAutoPaymentTableRes> result = new TxnRespResult<>();
        PrAutoPaymentTableRes res;
        try{
            prAutoPaymentTableRep.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prAutoPaymentTableRep.setUpdateBy(infoService.getUser().getUserName());
            res = prAutoPaymentTableService.addPrAutoPaymentTable(prAutoPaymentTableRep);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID编辑批扣配置", notes = "根据租户ID编辑批扣配置代偿回购参数表信息")
    @PutMapping("/")
    public TxnRespResult<PrAutoPaymentTableRes> modifyPrAutoPaymentTable(@RequestBody PrAutoPaymentTableRep prAutoPaymentTableRep) {
        TxnRespResult<PrAutoPaymentTableRes> result = new TxnRespResult<>();
        PrAutoPaymentTableRes res;
        try {
            prAutoPaymentTableRep.setUpdateBy(infoService.getUser().getUserName());
            res = prAutoPaymentTableService.modifyPrAutoPaymentTable(prAutoPaymentTableRep);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和产品ID删除表信息", notes = "根据租户ID和产品ID删除表信息")
    @DeleteMapping(value = "/{tenantId}/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "id", value = "渠道产品批扣配置表主键", required = true)
    })
    public TxnRespResult<Boolean> deletePrAutoPaymentTable(@NotNull @PathVariable String tenantId, @PathVariable String id) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = prAutoPaymentTableService.deletePrAutoPaymentTable(String.valueOf(infoService.getUser().getProjectId()), id);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

}
